package com.huazi.www.drug.drugy.servlet;


import com.alibaba.fastjson.JSON;
import com.huazi.www.drug.drugy.entity.Doctor_information;
import com.huazi.www.drug.drugy.service.DoctorService;
import com.huazi.www.drug.drugy.service.impl.DoctorServiceImpl;
import com.huazi.www.drug.drugy.util.Page;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @author 波——最帅的男人
 * @version 1.0
 * @date 2021/5/13 16:02
 */
public class DoctorServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //修改request域的编码集
        req.setCharacterEncoding("utf-8");
        //修改respones域的编码集
        resp.setContentType("text/html;charset=utf-8");
        //获取操作码  caozuo
        String caozuo = req.getParameter("caozuo");
        //new DoctorServiceImpl类的
        DoctorService doctorService = new DoctorServiceImpl();
        if (caozuo == null) {
            //获取传过来的值  条件  names
            String names = req.getParameter("stayDrug");
            if (names == null) {
                names = "";
            }
            //获取当前页
            String currentPage = req.getParameter("currentPage");
            if (currentPage == null) {
                currentPage = "1";
            }
            //new 调用page实现分页的方法
            Page<Doctor_information> page = doctorService.pageList2(Integer.parseInt(currentPage), names);
            //保存page对象
            req.setAttribute("pageList2", page);
            //保存条件  names
            req.setAttribute("names", names);
            //转发至doctorSelect.jsp页面
            req.getRequestDispatcher("drug/doctorSelect.jsp").forward(req, resp);
        } else {
            if (caozuo.equals("xinzeng")) {
                //获取ajax传过来的字符串  array1
                String array1 = req.getParameter("array1");
                //将这个字符串进行拆分  根据,号拆分
                String[] split = array1.split(",");
                // new 转换Date类的类  实现转换  yyyy-MM-dd
                SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
                Date date1 = null;
                try {
                    //调用方法进行转换
                    date1 = format.parse(split[9]);
                } catch (ParseException e) {
                    e.printStackTrace();
                }
                //将获取过来的值通过Doctor_information实现类的构造函数进行赋值
                Doctor_information d = new Doctor_information(split[0], split[1], Integer.parseInt(split[2]), split[3], split[4], split[5], split[6], split[7], split[8], date1);
                //调用业务层的插入医生方法
                boolean b = doctorService.insertDoctor(d);
                PrintWriter printWriter = resp.getWriter();
                String s = JSON.toJSONString(b);
                printWriter.print(s);
                printWriter.flush();
                printWriter.close();
            } else if (caozuo.equals("delete")) {
                //获取页面传过来的id2
                String id2 = req.getParameter("id2");
                //转换成int
                int i = Integer.parseInt(id2.trim());
                //调用业务层进行删除
                boolean b = doctorService.deleteDoctor(i);
                PrintWriter printWriter = resp.getWriter();
                String s = JSON.toJSONString(b);
                printWriter.print(s);
                printWriter.flush();
                printWriter.close();
            } else if (caozuo.equals("xiugai")) {
                //获取idd 传过来的id
                String idd = req.getParameter("idd");
                //医生id  转换int
                int id = Integer.parseInt(idd);
                //获取页面传过来的字符串
                String str33 = req.getParameter("str33");
                //通过,号分割成数组
                String[] split = str33.split(",");
                //转换成时间格式  yyyy-MM-dd
                SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
                Date date1 = null;
                try {
                    //调用转换方法
                    date1 = format.parse(split[9]);
                } catch (ParseException e) {
                    e.printStackTrace();
                }
                //将获取过来的值通过Doctor_information实现类的构造函数进行赋值
                Doctor_information d = new Doctor_information(id, split[0], split[1], Integer.parseInt(split[2]), split[3], split[4], split[5], split[6], split[7], split[8], date1);
                //调用业务层的更新方法
                boolean b = doctorService.updateDoctor(d);
                PrintWriter printWriter = resp.getWriter();
                String s = JSON.toJSONString(b);
                printWriter.print(s);
                printWriter.flush();
                printWriter.close();

            }
        }
    }
}
